package com.xiaohua.tlgorders.controller;

import com.xiaohua.tlgorders.entity.Order;
import com.xiaohua.tlgorders.entity.OrderGoods;
import com.xiaohua.tlgorders.service.GoodService;
import com.xiaohua.tlgorders.service.OrderGoodsService;
import com.xiaohua.tlgorders.service.OrderService;
import com.xiaohua.tlgorders.tools.Record;
import com.xiaohua.tlgorders.tools.Status;
import org.apache.ibatis.annotations.Param;
import org.apache.juli.logging.Log;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Random;
import java.util.logging.Logger;

/**
 *@Author 小花
 *@Date 2019/7/16 9:45
 **/
@RestController

public class OrderController {
    private SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Autowired
    GoodService goodService;

    private Random ra=new Random();
    @Autowired
    OrderGoodsService service;
    @Autowired
    OrderService orderService;
    /*
    *按照用户查询
     * @Author 小花
     * @Date 14:26 2019/7/19
     * @Param [userId, page, size, orderNo]
     **/
    @GetMapping("/ByUserId")
    public Record findByUserId(Integer userId,Integer page,Integer size){


        if(userId!=null){
            if(page==null||page==0){
                page=1;
            }
            if (size==null||size==0) {
                size=5;
            }

            return service.findUesrId(userId, page, size);
        }else{
            return  new Record(Status.IS_NOT_ACCOUNT,"请先登陆","null");
        }

    }
    /*
    *查看订单详情
     * @Author 小花
     * @Description //TODO
     * @Date 14:26 2019/7/19
     * @Param [userId, orderNo, addressId, page, size]
     *
     **/
    @GetMapping("/findDetail")
    public  Record findByDetail(Integer userId,String orderNo,Integer addressId,Integer page,Integer size){
        return service.findDetail(userId, orderNo,addressId,page,size);
    }
    /*
    *更改订单状态
     * @Author 小花
     * @Description //TODO
     * @Date 14:26 2019/7/19
     * @Param [orderNo, orderSatus, dataFlag]
     * @return com.xiaohua.tlgorders.tools.Record
     **/
    @GetMapping("/ModStatus")
    public Record updataStatus(Order order){
        System.out.println(order.getOrderStatus());
            if(order.getOrderStatus()==0){
                orderService.modIsPay(order.getOrderNo());
            }
return orderService.modOrderSatus(order);
    }
    /*
     *删除订单
     * @Author 小花
     * @Description //TODO
     * @Date 14:27 2019/7/19
     * @Param [orderNo]
     * @return com.xiaohua.tlgorders.tools.Record
     **/
    @GetMapping("/DelOrder")
    public Record DelOrder(Order order){
        return orderService.modDataFlag(order);
    }

    /*
    *按照订单号或者名字查询
     * @Author 小花
     * @Description //TODO
     * @Date 14:27 2019/7/19
     * @Param [orderNo, goodsName, userId, page, size]
     * @return com.xiaohua.tlgorders.tools.Record
     **/
@GetMapping("/ByNameOrNo")
    public  Record byNameOrNo(@Param("orderNo")String orderNo,  @Param("goodsName") String goodsName,@Param("userId") Integer userId,Integer page,Integer size){

    if(goodsName==null){
        goodsName="";
    }
    if(orderNo==null){
        orderNo="";
    }

    if(userId!=null){
        if (size==null||size==0) {
            size=5;
        }

        if(page==null){
            page=1;
        }

        return service.findByNoOrName(orderNo, goodsName, userId,page,size);
    }else{
        return  new Record(Status.IS_NOT_ACCOUNT,"请先登陆","null");
    }


}
/*
*根据订单状态查询
 * @Author 小花
 * @Description //TODO
 * @Date 14:28 2019/7/19
 * @Param [userId, orderSatus, page, size]
 * @return com.xiaohua.tlgorders.tools.Record
 **/
@GetMapping("/ByStatus")
    public  Record bySatus(Integer userId,Integer orderStatus, Integer page, Integer size){
    if(userId!=null){
        if(page==null||page==0){
            page=1;
        }
        if (size==null||size==0) {
            size=5;
        }
        return  service.findStatus(userId, orderStatus, page, size);
    }else{
        return  new Record(Status.IS_NOT_ACCOUNT,"请先登陆","null");
    }
}
/*
*确认支付
 * @Author 小花
 * @Description //TODO
 * @Date 14:28 2019/7/19
 * @Param [orderNo, isClosed]
 * @return com.xiaohua.tlgorders.tools.Record
 **/
@GetMapping("/modIsClosed")
public  Record modClosed(Order order){

    return orderService.modIsClosed(order) ;
}
/*
*新增订单+任务调度
 * @Author 小花
 * @Description //TODO
 * @Date 16:28 2019/8/2
 * @Param [order, goods]
 * @return com.xiaohua.tlgorders.tools.Record
 **/
    @GetMapping("/addOrder")
    public Record  addOrders(Order order, OrderGoods goods){

    String orderNo = System.currentTimeMillis() + String.valueOf((ra.nextInt(8999) + 1000));//获得订单号
    order.setOrderNo(orderNo);
    goods.setOrderNo(orderNo);
        Record re=new Record();
        //判断顶单号是否相同
    if(orderNo.equals(orderService.findRepeatOrder(orderNo))) {
            orderService.modIsPay(orderNo);     //更改支付状态

    }else{
        re  = orderService.addOrders(order, goods);
    }
        System.out.println("订单的"+order.toString());
        System.out.println("订单商品的"+goods.toString());

    if(order.getIsPay()==0){//判断是否支付
        auto();
    }
    return re;

}
   @Async
   @Scheduled(cron = "0 0/1 * * * ?")
    public void auto(){
        List<String> list=orderService.findTimeValue();
        for (String orderNo:list){

                orderService.batchMod(orderNo);

        }
}

//   @GetMapping("/aaa")
//    public void  ss(){
//        for (int i = 0; i <299999; i++) {
//            int count =ra.nextInt(89999999)+10000000;
//            goodService.addGoods("G"+count);
//            goodService.addGoods("G"+(count+10));
//            goodService.addGoods("G"+(count+20));
//        }
//    }
    /*
    *订单状态查询数量
     * @Author 小花
     * @Description //TODO
     * @Date 15:39 2019/8/5
     * @Param [orderNo, orderStatus]
     * @return com.xiaohua.tlgorders.tools.Record
     **/
            @GetMapping("/count")

    public Record orderCount(String orderNo,Integer orderStatus,Integer userId){

        return orderService.orderCount(orderNo,orderStatus,userId);
            }
    /*
     *按照名字或者订单号数量
     * @Author 小花
     * @Description //TODO
     * @Date 15:39 2019/8/5
     * @Param [orderNo, orderStatus]
     * @return com.xiaohua.tlgorders.tools.Record
     **/
@GetMapping("/otherCount")
public  Record otherCount(String orderNo, String goodsName, Integer userId){
                return service.byNameOrNoCount(orderNo, goodsName, userId);
}

/*
*新增退款原因
 * @Author 小花
 * @Description //TODO
 * @Date 15:19 2019/8/6
 * @Param [orderNo, rejectOtherReason]
 * @return com.xiaohua.tlgorders.tools.Record
 **/
@GetMapping("/ModReason")

public Record modReason(String orderNo,String rejectOtherReason){
    return orderService.ModReason(orderNo,rejectOtherReason);

}
/*
*判断是否退款
 * @Author 小花
 * @Description //TODO
 * @Date 15:21 2019/8/6
 * @Param [isRefund, orderNo]
 * @return com.xiaohua.tlgorders.tools.Record
 **/
@GetMapping("/ModIsRefund")
public  Record modIsRefund(Integer isRefund, String orderNo){
    return orderService.ModIsRefund(isRefund, orderNo);

}










































    @GetMapping("/aa")
    public void xx(){
        for (int i = 0; i < 100; i++) {
            Order order =new Order();
            OrderGoods goods=new OrderGoods();
            String orderNo = System.currentTimeMillis() + String.valueOf((ra.nextInt(8999) + 1000));//获得订单号
            order.setOrderNo(orderNo);
            goods.setOrderNo(orderNo);
            order.setUserId(2002);
            order.setOrderStatus(0);
            order.setIsPay(1);
            order.setDataFlag(1);
            order.setAddressId(1);
            goods.setGoodsId(1);
            goods.setGoodsNum(2);
            goods.setGoodsSpecId(1);
            goods.setAttributeId(1);
            goods.setGoodsMoney(2000);
            goods.setDeliverMoney(50);
            goods.setTotalMoney(2050);
            System.out.println("----------------------------");
            System.out.println( orderService.addOrders(order,goods));
        }

    }
}

